package cn.charmaine.product.service.impl;

import cn.charmaine.basic.Exception.CustomException;
import cn.charmaine.basic.util.BaiduAuditUtils;
import cn.charmaine.basic.util.LoginContext;
import cn.charmaine.basic.util.PageList;
import cn.charmaine.org.domain.Employee;
import cn.charmaine.org.domain.Logininfo;
import cn.charmaine.org.mapper.IEmployeeMapper;
import cn.charmaine.basic.base.service.impl.BaseServiceImpl;
import cn.charmaine.pet.domain.Product;
import cn.charmaine.pet.domain.ProductDetail;
import cn.charmaine.pet.domain.ProductOnlineAuditLog;
import cn.charmaine.pet.mapper.ProductDetailMapper;
import cn.charmaine.pet.mapper.ProductMapper;
import cn.charmaine.pet.mapper.ProductOnlineAuditLogMapper;
import cn.charmaine.pet.service.IProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xmy
 * @since 2022-07-22
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Autowired
    private IEmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void add(Product product) {
        //设置基本信息
        product.setSalecount(0L);
        product.setState(0L);
        product.setCreatetime(new Date());
        //先保存product
        productMapper.insert(product);
        //再保存详情
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());//得到添加宠物后的id，设置到宠物详情中去
        productDetailMapper.insert(detail);
    }

    @Override
    public void update(Product product) {
        //先删除再修改
        Product p = productMapper.selectByProductName(product.getName());
        deleteById(p.getId());
        add(product);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        //删除详情里的数据
        productDetailMapper.deleteByProductId(id);
        //通过id删除宠物
        productMapper.deleteById(id);

    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        //先批量删除detail里的数据
        productDetailMapper.batchDeleteByProductIds(ids);
        //再删除product里的数据
        productMapper.batchDelete(ids);
    }

   @Override
    public void auditOnsale(List<Long> ids, HttpServletRequest request) {
        //创建上架日志对象
        ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
        //利用工具类解析，得到logininfo
        Logininfo logininfo = LoginContext.getCurrentUser(request);
        //利用logininfo的id查询对应的employee
        Employee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
        //设置上架日志的audit_id
        productOnlineAuditLog.setAuditId(employee.getId());

        //空参数校验
        if(StringUtils.isEmpty(ids)){
            throw new CustomException("请选择需要上架的服务！");
        }
        for (Long id : ids) {
            Product product = productMapper.selectById(id);
            productOnlineAuditLog.setProductId(id);
            productOnlineAuditLog.setAuditTime(new Date());
            //百度ai审核
            if(!BaiduAuditUtils.TextCensor(product.getName())){//审核没通过
                productOnlineAuditLog.setState(0);//设置审核状态为失败
                productOnlineAuditLog.setNote("审核失败！");
                productOnlineAuditLogMapper.insert(productOnlineAuditLog);
                throw new CustomException(product.getName()+"服务名不符合规范，请重新输入!");
            }
            //审核成功的情况
            productOnlineAuditLog.setState(1);//设置审核状态为成功
            productOnlineAuditLog.setNote("审核成功！");
            productOnlineAuditLogMapper.insert(productOnlineAuditLog);
            //修改宠物信息
            product.setState(1L);//设置宠物为上架
            product.setOnsaletime(new Date());//设置宠物上架时间
            productMapper.update(product);
        }

    }

    @Override
    public void auditOffsale(List<Long> ids,Date date) {
        if(StringUtils.isEmpty(ids)){
            throw new CustomException("请选择需要下架的商品！");
        }
        productMapper.batchUpdateProductState(ids,date);
    }
    
}
